﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CodeGame
{
    class World : EntityContainer
    {
        public float CameraX = 0;
        public float CameraY = 0;

        protected float CameraSpeed = 2f;

        public virtual Blocks GetPointData(int x, int y)
        {
            return Blocks.Air;
        }

        public virtual void LoadNetworkWorld(byte[] data)
        {

        }

        public virtual void DrawWorld()
        {

        }

        public virtual void LoadWorld(byte[,] world)
        {

        }

        public override void Update()
        {
            if (Singletons.IsKeyboardLocked)
            {
                return;
            }

            KeyboardState state = Keyboard.GetState();

            if (state.IsKeyDown(Keys.Up))
            {
                this.CameraY += this.CameraSpeed;
            }

            if (state.IsKeyDown(Keys.Down))
            {
                this.CameraY += -this.CameraSpeed;
            }

            if (state.IsKeyDown(Keys.Left))
            {
                this.CameraX += this.CameraSpeed;
            }

            if (state.IsKeyDown(Keys.Right))
            {
                this.CameraX += -this.CameraSpeed;
            }

            base.Update();
        }

        public override void Draw()
        {
            this.DrawWorld();

            base.Draw();
        }

        public void TeleportCamera(int x, int y)
        {
            this.CameraX = x;
            this.CameraY = y;
        }

        public void DrawImage(string texture, int x, int y, Color tint)
        {
            Singletons.Content.DrawImage(texture, x + (int)this.CameraX, y + (int)this.CameraY, tint);
        }

        public void DrawImage(string texture, int x, int y, float angle, Color tint)
        {
            Singletons.Content.DrawImage(texture, x + (int)this.CameraX, y + (int)this.CameraY, MathHelper.ToRadians(angle), 1.0f, tint);
        }

        public override void AddEntity(Entity ent)
        {
            base.AddEntity(ent);
        }

        public void EmptyEntities()
        {
            this.Entities.Clear();
        }

        public string ReturnAllID()
        {
            int x = 0;
            string str = "";

            foreach (Entity item in this.Entities)
            {
                if (item is GameEntity)
                {
                    GameEntity ent = (GameEntity)item;

                    str += ent.ID + ", ";
                    
                    x++;
                    if (x > 6)
                    {
                        str += "\n";
                        x = 0;
                    }
                }
            }

            return str;
        }
    }
}
